<?php
namespace app\common\model;
use think\Cache;

class MemberApply extends Base
{
    static $status_remark = [
            0 => '待处理',
            1 => '通过',
            2 => '驳回',
        ];

	// 数据完成字段
	protected $auto = [];
	protected $insert = ['create_time'];
	protected $update = ['deal_time'];
    // 只读字段
    //protected $readonly = ['id','mid','create_time'];

	protected function setCreateTimeAttr()
    {
        return time();
    }
    protected function setDealTimeAttr()
    {
        return time();
    }

    public static function status($key=null){
        if(is_null($key)){
            return self::$status_remark;
        }else{
            return self::$status_remark[$key];
        }
    }

    /**
     * 获取分页列表
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public function getPageDatas($where,$page,$perpagenum){
        
        $start = ($page-1)*$perpagenum;
        
        $whereData=[];
        if(!empty($where)){
            foreach($where as $k => $wh):
                $whereData['a.'.$k] = $wh;
            endforeach;
        }
        
        $rows = $this
                ->alias('a')
                ->join('member b','a.uid=b.id')
                ->where($whereData)
                ->field('a.*,b.mid mid')
                ->limit($start,$perpagenum)
                ->order('a.id','DESC')->select();

        $count = $this->alias('a')->where($whereData)->count();
        $data = [];
        if($count){
            foreach($rows as $row):
                $row = $row->toArray();
                $data[] = $this->setField($row);
            endforeach;
        }

        $total_page = ceil($count/$perpagenum);
        return [
            'total_page' => $total_page,
            'curent_page' => $page,
            'count' => $count,
            'data' => $data
        ];
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public function setField($row){
        if(isset($row['create_time'])){
            $row['create_date'] = date("Y-m-d H:i",$row['create_time']);
        }
        if(isset($row['deal_time'])){
            $row['deal_date'] = $row['deal_date']?date("Y-m-d H:i",$row['deal_time']):'未处理';
        }
        if(isset($row['status'])){
            $row['status_remark'] = self::$status_remark[$row['status']];
        }
        return $row;
    }
	
    public function getOneDataByUid($uid){
        $res = $this->where(['uid'=>$uid])->find();
        if($res){
            $res = $this->setField($res);
            return $res;
        }else{
            return false;
        }
    }

    public function saveOneData($data,$uid){
        // 是否存在未审核申请
        if($this->isExist($uid)){
            return false;
        }
        return $this->allowField(true)->save($data);
    }

    public function isExist($uid){
        if($result = $this->getOneDataByUid($uid)){
            if($result['status']==1){
                $this->error = '已经申请过';
            }else{
                $this->error = '已经申请，等待审核中';
            }
            return true;
        }else{
            return false;
        }
    }

}
